<?php
//微信机器人模板类
//Author: Midoks
//Author URI: http://midoks.cachecha.com/
class weixin_robot_tpl{
	
  	/**
     * @func 共同头部
 	 * @param $string xml
	 * @ret xml
	 */
  	public function common($xml){
  		$common ="<xml><ToUserName><![CDATA[%s]]></ToUserName>
				  <FromUserName><![CDATA[%s]]></FromUserName>
				  <CreateTime>%s</CreateTime>
				  {$xml}
				  </xml>";
      	return $common;
	}

	//解决bug
	public function common2($xml, $obj){
		$common ='<ToUserName><![CDATA[%s]]></ToUserName>
				  <FromUserName><![CDATA[%s]]></FromUserName>
				  <CreateTime>%s</CreateTime>';
		$c2 = sprintf($common,
					  $obj->fromUserName,
                      $obj->toUserName,
					  $obj->time);
		return "<xml>{$c2}{$xml}</xml>";
	}

	/**
	 *	@func 返回信息(文本消息)
     *  @ret string xml
	 */
	public function text(){
		$textTpl = '<MsgType><![CDATA[%s]]></MsgType>
					<Content><![CDATA[%s]]></Content>
					<MsgId>0</MsgId>';
    	return $this->common($textTpl);
	}
  
  	/**
	 *	@func 返回信息(图片消息)
     *  @ret string xml
	 */
	public function pic(){
		$picTpl = "<MsgType><![CDATA[%s]]></MsgType>
 				   <PicUrl><![CDATA[%s]]></PicUrl>
 				   <MsgId>0</MsgId>";
    	return $this->common($picTpl);
	}
  	
  	/**
	 *	@func 返回信息(图文)
	 *  @param array $info 图片信息
	 *  @param object $obj 对象
     *  @ret string xml
	 */
  	public function textpic($info, $obj){
      	$num = count($info);
      	$tp_xml = '<item><Title><![CDATA[%s]]></Title>
 					<Description><![CDATA[%s]]></Description>
 					<PicUrl><![CDATA[%s]]></PicUrl>
 					<Url><![CDATA[%s]]></Url></item>';
      	$tp_str = '';
      	foreach($info as $n=>$v){
        	$tp_str .= sprintf($tp_xml,
                    		  $v['title'],//标题
                    		  $v['desc'],//描述
                    		  $v['pic'],//图片地址
                              $v['link']//链接地址
                   		);
        }
        //var_dump($tp_str);
  		$textPicTpl = "<MsgType><![CDATA[news]]></MsgType>
					<ArticleCount>{$num}</ArticleCount>
 					<Articles>{$tp_str}</Articles>";
      	$xml = $this->common2($textPicTpl, $obj);
		//var_dump($xml);var_dump($obj);
		//var_dump($resultStr);
      	return $xml;
  	}
  
  	public function voice(){
        $voiceTpl = '<MsgType><![CDATA[music]]></MsgType>
 					<Music>
 						<Title><![CDATA[%s]]></Title>
 						<Description><![CDATA[%s]]></Description>
 						<MusicUrl><![CDATA[%s]]></MusicUrl>
 						<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
	 				</Music>';
      	return $this->common($voiceTpl);
  	}
  

}
?>
